﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;
using DicitiControlLib;
using AxDicitiControlLib;
using System.IO;
using System.Runtime.InteropServices;
using DevExpress.XtraNavBar;


namespace Emergency.Controls
{
    public partial class ExercisePane : DevExpress.XtraEditors.XtraUserControl
    {
        //绘图相关
        private bool isFinish = false;
        private string DrawType = string.Empty;

        private AxDCT3DWindow d3 = null;
        private IConfig _config = null;
        private IInformationTree _it = null;

        private I3DArrow routeArrow = null;
        private I3DPolygon effededArea = null;

        XElement[] m_planContent = null;
        List<string> currentViewObjects = null;

        public ExercisePane()
        {
            InitializeComponent();
            d3 = this.DicitiMain;
            _config = d3.config;
            _it = d3.infoTree;

            IInitializeResult res = _config.Initialize();
            if (res == null || res.isOK == false)
            {

                MessageBox.Show("三维控件初始化失败!");

            }
        }


        private void LoadPlanFile(string planPath)
        {
            currentViewObjects = new List<string>();
            int itemCount = nbgScene.ItemLinks.Count;
            for (int i = 0; i < itemCount; i++)
            {
                nbgScene.ItemLinks.RemoveAt(0);
            }
            XDocument planDoc = XDocument.Load(planPath);
            string planName = planDoc.Root.Attribute("label").Value;
            NavBarItem planItem = new NavBarItem(planName);
            nbgScene.ItemLinks.Add(new NavBarItemLink(planItem));

            m_planContent = planDoc.Root.Descendants(XName.Get("viewPack")).ToArray();
            itemCount = nbgScene.ItemLinks.Count;
            for (int i = 0; i < itemCount; i++)
            {
                nbgScene.ItemLinks.RemoveAt(0);
            }
            for (int i = 0; i < m_planContent.Length; i++)
            {
                NavBarItem item = new NavBarItem(m_planContent[i].Attribute("label").Value);
                item.LinkClicked += new NavBarLinkEventHandler(item_LinkClicked);
                nbgScene.ItemLinks.Add(new NavBarItemLink(item));

            }

        }

        private void item_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            IEnumerable<XElement> elements = from tempElement in m_planContent where tempElement.Attribute("label").Value == (sender as NavBarItem).Caption select tempElement;
            XElement selectedElement = elements.ToArray()[0];
            for (int i = 0; i < currentViewObjects.Count; i++) 
            {
                _it.SetVisibility(currentViewObjects[i], Convert.ToByte(@"0"));
            }
            if (selectedElement.Attribute("viewSpotPath").Value != "")
            {
                string viewSpotID = _it.FindItem(selectedElement.Attribute("viewSpotPath").Value);
                if (selectedElement.Attribute("type").Value == "viewPort")
                    d3.camera.FlyToObject(viewSpotID, ActionCode.AC_FLYTO);
                else if (selectedElement.Attribute("type").Value == "animation")
                {
                    IObject obj = d3.objectManager.GetObject(viewSpotID);
                    IDynamicObject dyno = obj as IDynamicObject;
                    bool isPermernent = bool.Parse(selectedElement.Attribute("permernent").Value);
                    if (isPermernent == false)
                        currentViewObjects.Add(obj.objectId);
                    _it.SetVisibility(obj.objectId, Convert.ToByte(@"1"));
                    d3.camera.FlyToObject(obj.objectId, ActionCode.AC_FLYTO);
                    dyno.RestartRoute(0);
                }

                SetupViewObjects(selectedElement);
            }


        }
        private void SetupViewObjects(XElement viewPack)
        {
            XElement[] viewObjects = viewPack.Descendants(XName.Get("ViewObject")).ToArray();

            bool isPermernent = bool.Parse(viewPack.Attribute("permernent").Value);
            for (int i = 0; i < viewObjects.Length; i++)
            {
                XElement viewObject = viewObjects[i];
                string objectPath = viewObject.Attribute("path").Value;

                string objectID = _it.FindItem(objectPath);
                if (isPermernent == false)
                    currentViewObjects.Add(objectID);
                _it.SetVisibility(objectID, Convert.ToByte(@"1"));

            }
        }



        public void SetDrawMode(string mode)
        {
            this.isFinish = false;
            DrawType = mode;
        }

        private void DicitiMain_MouseMoveEvent(object sender, _IDCT3DWindowEvents_MouseMoveEvent e)
        {
            double dx = double.NaN, dy = double.NaN, dz = double.NaN;
            d3.render.ScreenToTerrain(e.x, e.y, out dx, out dy, out dz);
            if (DrawType == "route")
            {

                if (isFinish == false)
                {
                    // if (effededArea != null)
                    //routeArrow.ModifyVertex(routeArrow.numofVertices - 1, dx, dy, dz, 0);

                }
            }
            else if (DrawType == "effectedArea")
            {
                if (isFinish == false)
                {
                    if (effededArea != null)
                        effededArea.ModifyVertex(effededArea.numofVertices - 1, dx, dy, dz, 0);

                }
            }
           
        }

        private void DicitiMain_LButtonUp(object sender, _IDCT3DWindowEvents_LButtonUpEvent e)
        {
            


            double dx = double.NaN, dy = double.NaN, dz = double.NaN;
            d3.render.ScreenToTerrain(e.x, e.y, out dx, out dy, out dz);
            if (DrawType == "route")
            {

                if (isFinish == false)
                {

                    if (routeArrow != null)
                    {

                    }
                    else
                    {
                        routeArrow = d3.objectManager.Create3DArrow(dx, dy, dz, 0, 50, ThreeDArrowStyle.TD_ARROW_STYLE_1, 100, Utils.RGBA(255, 0, 0, 255), 0.2, Utils.RGBA(222, 147, 33, 255), HeightStyleCode.HSC_TERRAIN_ABSOLUTE, _it.rootId, "3DArrow");
                    }

                    //routeArrow.AddVertex(dx, dy, dz, 0);

                }
                else
                {
                    //routeArrow = d3.objectManager.Create3DArrow(dx, dy, dz, 0, 50, ThreeDArrowStyle.TD_ARROW_STYLE_1, 100, Utils.RGBA(255, 0, 0, 255), 0.2, Utils.RGBA(222, 147, 33, 255), HeightStyleCode.HSC_TERRAIN_ABSOLUTE, _it.rootId, "3DArrow");

                    if (routeArrow != null)
                    {

                        //routeArrow.AddVertex(dx, dy, dz, 0);

                        //routeArrow.AddVertex(dx, dy, dz, 0);

                        isFinish = true;
                    }
                }
            }
            else if (DrawType == "effectedArea")
            {
                if (isFinish == false)
                {

                    if (effededArea != null)

                        effededArea.AddVertex(dx, dy, dz, 0);
                    else
                    {
                        effededArea = d3.objectManager.Create3DPolygon(null, 50, Utils.RGBA(255, 0, 0, 125), 0.2, Utils.RGBA(222, 147, 33, 255), HeightStyleCode.HSC_TERRAIN_ABSOLUTE, _it.rootId, "3DPolygon");
                    }

                }
                else
                {
                    

                    if (effededArea != null)
                    {

                        effededArea.AddVertex(dx, dy, dz, 0);

                        effededArea.AddVertex(dx, dy, dz, 0);

                        isFinish = true;
                    }
                }
            }
            else if (DrawType == "model")
            {
                if (isFinish == false)
                {
                    //string fileName = @"C:\Samples\Media\Model\Buildings\Building1.gol";
                    string fileName = Path.Combine(Application.StartupPath, @"DEMO0815\data\Firetruck.3DS");
                    IModel _pModel = d3.objectManager.CreateModel(dx, dy, 0.0, fileName, 0, 0, 0, 1, HeightStyleCode.HSC_TERRAIN_RELATIVE, ModelTypeCode.MT_NORMAL, _it.rootId, "");
                }
               

            }
        }

        private void DicitiMain_RButtonUp(object sender, _IDCT3DWindowEvents_RButtonUpEvent e)
        {
            isFinish = true;
            effededArea = null;
            routeArrow = null;
        }
        //窗体加载完成
        private void ExercisePane_Load(object sender, EventArgs e)
        {
           // Roll();
            timerCmd.Start();
            timerNotice.Start();
            LoadPlanFile(Path.Combine(Application.StartupPath, "火灾.plan"));
        }

        private void timerCmd_Tick(object sender, EventArgs e)
        {
            //Point LPos = new Point(this.lblNotice.Location.X, this.lblNotice.Location.Y);
            //if (LPos.X > panelNotice.Location.X - this.lblNotice.Width)
            //{
            //    this.lblNotice.Location = new Point(LPos.X - 10, LPos.Y);
            //    return;
            //}
            //else
            //{
            //    this.lblNotice.Location = new Point(panelNotice.Location.X + panelNotice.Width, lblNotice.Location.Y);

            //}
        }
       

        private void timerNotice_Tick(object sender, EventArgs e)
        {
            //Point LPos = new Point(this.lblCmd.Location.X, this.lblCmd.Location.Y);
            //if (LPos.X > panelCommand.Location.X - this.lblCmd.Width)
            //{
            //    this.lblCmd.Location = new Point(LPos.X - 10, LPos.Y);
            //    return;
            //}
            //else
            //{
            //    this.lblCmd.Location = new Point(panelCommand.Location.X + panelCommand.Width, panelCommand.Location.Y);

            //}
        }
    }
}
